# -*- coding: utf-8 -*-
import scrapy

from novel.items import NovelItem


class BiqugewaSpider(scrapy.Spider):
    name = 'biqugewa'
    allowed_domains = ['www.biqugewa.com/']

    page = 0
    def start_requests(self):
        yield scrapy.Request(url='http://www.biqugewa.com/sort/all/'+str(self.page)+".html", callback=self.parse)

    def parse(self, response):
        href=response.css('a.novelname::attr(href)').extract()

        for a in href:
            yield scrapy.Request(response.urljoin(a),callback=self.parse_novel)

        if len(href)==0:
            yield scrapy.Request(url='http://www.biqugewa.com/sort/all/'+str(self.page+1)+".html", callback=self.parse)


    def parse_novel(self,response):
        item=NovelItem()

        name=response.css('h1::text').extract_first()
        li=response.css('div.novelinfo-l >  ul > li')
        info=li.css('a::text').extract()
        author=info[0]
        type=info[1]
        latest=info[2]
        #更新时间
        update=li[-1].css('::text').extract_first().split('：')[-1]
        #封面
        img=response.css('div.novelinfo-r > a > img::attr(src)').extract_first()
        img_href=response.urljoin(img)
        introduction=response.css('div.novelintro::text').extract_first()

        item['name']=name
        item['type']=type
        item['author']=author
        item['latest']=latest
        item['update']=update
        item['cover']=img_href
        item['introduction']=introduction
        item['chapter']=[]
        #章节
        chapter_list=response.css('div.mt20 > div.body>ul')[-1].css('li > a::attr(href)').extract()
        for chapter in chapter_list:
            yield  response.follow(chapter,callback=self.parse_chapter,meta={'item':item})


    def parse_chapter(self,response):
        item=response.meta['item']
